package com.game.league.util;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.fragment.app.FragmentActivity;

import com.game.base.utils.toast.ToastUtil;
import com.game.base.view.image.viewer.ImagePreview;
import com.game.base.x5.web.util.WebUtil;
import com.game.league.MainActivity;
import com.game.league.articles.dialog.ExchangeDialog;
import com.game.league.articles.table.Articles;
import com.game.league.entrance.login.LoginActivity;
import com.game.league.mine.cash.CashExtractActivity;
import com.game.league.mine.detail.FlowBillActivity;
import com.game.league.mine.set.AboutActivity;
import com.game.league.mine.set.SettingActivity;
import com.game.league.task.audit.AuditTaskActivity;
import com.game.league.task.detail.TaskDetailActivity;
import com.game.league.task.dialog.SignDialog;
import com.game.league.task.sub.TaskListActivity;
import com.game.league.task.table.Task;
import com.game.league.web.WebActivity;

import java.util.List;

/**
 * 页面跳转
 * Created by Su on 2019/5/21.
 */
public class SkipUtil {

    // ================================================== 特殊调用 ==========================================

    /**
     * 打开随机QQ客服
     */
    public static void showQQService(Context context, String qq) {
        try {
            String url = "mqqwpa://im/chat?chat_type=wpa&uin=" +
                    (TextUtils.isEmpty(qq) ? "396012819" : qq);
            Uri uri = Uri.parse(url);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            if (intent.resolveActivity(context.getPackageManager()) != null) {
                context.startActivity(intent);
            } else {
                ToastUtil.showShort("未安装QQ");
            }
        } catch (Exception e) {
            ToastUtil.showShort("打开QQ客服出错，请稍候");
        }
    }

    /**
     * 图片大图预览
     */
    public static void showBigImgPreview(Context context, int index, List<String> imgList) {
        if (imgList == null || imgList.isEmpty()) return;
        ImagePreview
                .getInstance()
                .setContext(context)
                .setIndex(index)
                .setEnableDragClose(true)
                .setImageList(imgList)
                .start();
    }
    // =============================================== 普通调用 ==========================================

    public static void show(Context context, Intent intent) {
        context.startActivity(intent);
    }

    public static void show(Context context, Class clz, int flag) {
        Intent intent = new Intent(context, clz);
        if (flag != -1) intent.addFlags(flag);
        show(context, intent);
    }

    public static void show(Context context, Class clz) {
        show(context, clz, -1);
    }

    /**
     * 显示activity并清空栈里其他activity
     *
     * @param activity 要启动的activity
     */
    private static void showActivityClearTask(Context context, Class activity) {
        show(context, activity, Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
    }

    /**
     * 调用改方法启动activity，可将应用从后台唤起到前台，
     */
    public static void showActivityFromBack(Context context, Class activity) {
        show(context, activity, Intent.FLAG_ACTIVITY_NEW_TASK);
    }

    /**
     * 打开WebView
     */
    public static void showWebActivity(Context context, String title, String url) {
        showWebActivity(context, title, url,"", false);
    }

    public static void showWebActivity(Context context, String url, boolean outSide) {
        showWebActivity(context, "", url,"", outSide);
    }

    public static void showWebActivity(Context context, String title, String url, String data, boolean outSide) {
        // 外部浏览器打开
        if (outSide) {
            WebUtil.openUrlOnBrowser(context, url);
            return;
        }
        // 内部WebView打开
        Intent intent = new Intent(context, WebActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("title", title);
        bundle.putString("url", url);
        bundle.putString("data", data);
        intent.putExtras(bundle);
        show(context, intent);
    }

    public static void openTaoBao(Context context) {
        Intent intent = context.getPackageManager().getLaunchIntentForPackage("com.taobao.taobao");
        context.startActivity(intent);
    }

    /**
     * 签到成功弹框
     */
    public static void showSignDialog(FragmentActivity activity) {
        SignDialog signDialog = new SignDialog();
        signDialog.showDialog(activity);
    }

    /**
     * 物品兑换弹框
     */
    public static void showExchangeDialog(FragmentActivity activity, Articles articles) {
        ExchangeDialog dialog = new ExchangeDialog(articles);
        dialog.showDialog(activity);
    }

    /**
     * 跳转到主页并清除栈里的其他页面
     */
    public static void showMainClearTask(Context context) {
        showActivityClearTask(context, MainActivity.class);
    }

    /**
     * 打开注册登录页
     */
    public static void showLoginActivity(Context context) {
        showActivityClearTask(context, LoginActivity.class);
    }

    /**
     * 打开设置页
     */
    public static void showSettingActivity(Context context) {
        show(context, SettingActivity.class);
    }

    /**
     * 打开关于页
     */
    public static void showAboutActivity(Context context) {
        show(context, AboutActivity.class);
    }

    /**
     * 提现申请页
     */
    public static void showCashExtractActivity(Context context) {
        show(context, CashExtractActivity.class);
    }

    /**
     * 任务详情页
     */
    public static Task currentTask;    // BmobObject经过Parcelable后，无法正确生成数据库表数据

    public static void showTaskDetailActivity(Context context, Task task) {
        currentTask = task;
        show(context, TaskDetailActivity.class);
    }

    /**
     * 任务审核进度
     */
    public static void showAuditTaskActivity(Context context) {
        show(context, AuditTaskActivity.class);
    }

    /**
     * 任务页
     */
    public static void showTaskListActivity(Context context) {
        show(context, TaskListActivity.class);
    }

    /**
     * 收支明细页
     */
    public static void showFlowBillActivity(Context context) {
        show(context, FlowBillActivity.class);
    }
}
